﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Slja.DBUtility;//Please add references
namespace Slja.DAL
{
    /// <summary>
    /// 数据访问类:ClientModel
    /// </summary>
    public partial class ClientDAL
    {
        public ClientDAL()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ClientName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from biz_Client");
            strSql.Append(" where ClientName=@ClientName ");
            SqlParameter[] parameters = {
					new SqlParameter("@ClientName", SqlDbType.NVarChar,40)			};
            parameters[0].Value = ClientName;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Slja.Model.ClientModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into biz_Client(");
            strSql.Append("ClientName,Pinyin,PyShort,LinkMan,ClientType)");
            strSql.Append(" values (");
            strSql.Append("@ClientName,@Pinyin,@PyShort,@LinkMan,@ClientType)");
            SqlParameter[] parameters = {
					new SqlParameter("@ClientName", SqlDbType.NVarChar,40),
					new SqlParameter("@Pinyin", SqlDbType.NVarChar,30),
					new SqlParameter("@PyShort", SqlDbType.NVarChar,20),
					new SqlParameter("@LinkMan", SqlDbType.Int,4),
					new SqlParameter("@ClientType", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.ClientName;
            parameters[1].Value = model.Pinyin;
            parameters[2].Value = model.PyShort;
            parameters[3].Value = model.LinkMan;
            parameters[4].Value = model.ClientType;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Slja.Model.ClientModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update biz_Client set ");
            strSql.Append("Pinyin=@Pinyin,");
            strSql.Append("PyShort=@PyShort,");
            strSql.Append("LinkMan=@LinkMan,");
            strSql.Append("ClientType=@ClientType");
            strSql.Append(" where ClientName=@ClientName ");
            SqlParameter[] parameters = {
					new SqlParameter("@Pinyin", SqlDbType.NVarChar,30),
					new SqlParameter("@PyShort", SqlDbType.NVarChar,20),
					new SqlParameter("@LinkMan", SqlDbType.Int,4),
					new SqlParameter("@ClientType", SqlDbType.NVarChar,20),
					new SqlParameter("@ClientID", SqlDbType.Int,4),
					new SqlParameter("@ClientName", SqlDbType.NVarChar,40)};
            parameters[0].Value = model.Pinyin;
            parameters[1].Value = model.PyShort;
            parameters[2].Value = model.LinkMan;
            parameters[3].Value = model.ClientType;
            parameters[4].Value = model.ClientID;
            parameters[5].Value = model.ClientName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ClientName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from biz_Client ");
            strSql.Append(" where ClientName=@ClientName ");
            SqlParameter[] parameters = {
					new SqlParameter("@ClientName", SqlDbType.NVarChar,40)			};
            parameters[0].Value = ClientName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ClientNamelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from biz_Client ");
            strSql.Append(" where ClientName in (" + ClientNamelist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Slja.Model.ClientModel GetModel(string ClientName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ClientID,ClientName,Pinyin,PyShort,LinkMan,ClientType from biz_Client ");
            strSql.Append(" where ClientName=@ClientName ");
            SqlParameter[] parameters = {
					new SqlParameter("@ClientName", SqlDbType.NVarChar,40)			};
            parameters[0].Value = ClientName;

            Slja.Model.ClientModel model = new Slja.Model.ClientModel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        public void AssignModel(DataRow dr, Model.ClientModel model)
        {
            if (dr["ClientID"] != null && dr["ClientID"].ToString() != "")
            {
                model.ClientID = int.Parse(dr["ClientID"].ToString());
            }
            if (dr["ClientName"] != null && dr["ClientName"].ToString() != "")
            {
                model.ClientName = dr["ClientName"].ToString();
            }
            if (dr["Pinyin"] != null && dr["Pinyin"].ToString() != "")
            {
                model.Pinyin = dr["Pinyin"].ToString();
            }
            if (dr["PyShort"] != null && dr["PyShort"].ToString() != "")
            {
                model.PyShort = dr["PyShort"].ToString();
            }
            if (dr["LinkMan"] != null && dr["LinkMan"].ToString() != "")
            {
                model.LinkMan = int.Parse(dr["LinkMan"].ToString());
            }
            if (dr["ClientType"] != null && dr["ClientType"].ToString() != "")
            {
                model.ClientType = dr["ClientType"].ToString();
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ClientID,ClientName,Pinyin,PyShort,LinkMan,ClientType ");
            strSql.Append(" FROM biz_Client ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by ClientID ASC");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ClientID,ClientName,Pinyin,PyShort,LinkMan,ClientType ");
            strSql.Append(" FROM biz_Client ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM biz_Client ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ClientName desc");
            }
            strSql.Append(")AS Row, T.*  from biz_Client T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "biz_Client";
            parameters[1].Value = "ClientName";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

